package org.example.oauth2.github_login;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/index.html")
    public String index(){
        return "index";
    }

    @GetMapping("/authorization_code")
    public String authorization_code(String code, Model model) throws JsonProcessingException {

        Map<String, String> map = new HashMap<>();
        map.put("client_id", "Ov23lirOGjZkkBKOYOS7");
        map.put("client_secret", "xxx");
        map.put("state", "wangnian");
        map.put("code", code);
        map.put("redirect_uri", "http://localhost:8081/authorization_code");
        Map<String,String> resp = restTemplate.postForObject("https://github.com/login/oauth/access_token", map, Map.class);
        System.out.println(resp);
        HttpHeaders httpheaders = new HttpHeaders();
        httpheaders.add("Authorization", "token " + resp.get("access_token"));
        HttpEntity<?> httpEntity = new HttpEntity<>(httpheaders);
        ResponseEntity<Map> exchange = restTemplate.exchange("https://api.github.com/user", HttpMethod.GET, httpEntity, Map.class);
        Map<String,Object> body = exchange.getBody();
        if (body != null) {
            String login = (String) body.get("login");
            String name = (String) body.get("name");
            System.out.println("body = " + new ObjectMapper().writeValueAsString(body));
            model.addAttribute("msg", name + ":" + login);
        }
        return "forward:/index.html";

    }

}
